<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属用户" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="所属用户"></el-input>
    </el-form-item>
    <el-form-item label="账户" prop="account">
      <el-input v-model="dataForm.account" placeholder="账户"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="授权码" prop="code">
      <el-input v-model="dataForm.code" placeholder="授权码"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型"></el-input>
    </el-form-item>
    <el-form-item label="默认权重" prop="defaultWeight">
      <el-input v-model="dataForm.defaultWeight" placeholder="默认权重"></el-input>
    </el-form-item>
    <el-form-item label="健康值" prop="health">
      <el-input v-model="dataForm.health" placeholder="健康值"></el-input>
    </el-form-item>
    <el-form-item label="" prop="state">
      <el-input v-model="dataForm.state" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="短信平台" prop="smsPlatform">
      <el-input v-model="dataForm.smsPlatform" placeholder="短信平台"></el-input>
    </el-form-item>
    <el-form-item label="" prop="phoneArea">
      <el-input v-model="dataForm.phoneArea" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="header">
      <el-input v-model="dataForm.header" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          account: '',
          password: '',
          code: '',
          phone: '',
          type: '',
          defaultWeight: '',
          health: '',
          state: '',
          createTime: '',
          smsPlatform: '',
          phoneArea: '',
          header: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '所属用户不能为空', trigger: 'blur' }
          ],
          account: [
            { required: true, message: '账户不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '授权码不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          defaultWeight: [
            { required: true, message: '默认权重不能为空', trigger: 'blur' }
          ],
          health: [
            { required: true, message: '健康值不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          smsPlatform: [
            { required: true, message: '短信平台不能为空', trigger: 'blur' }
          ],
          phoneArea: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          header: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/mailaccountpool/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.mailaccountpool.userId
                this.dataForm.account = data.mailaccountpool.account
                this.dataForm.password = data.mailaccountpool.password
                this.dataForm.code = data.mailaccountpool.code
                this.dataForm.phone = data.mailaccountpool.phone
                this.dataForm.type = data.mailaccountpool.type
                this.dataForm.defaultWeight = data.mailaccountpool.defaultWeight
                this.dataForm.health = data.mailaccountpool.health
                this.dataForm.state = data.mailaccountpool.state
                this.dataForm.createTime = data.mailaccountpool.createTime
                this.dataForm.smsPlatform = data.mailaccountpool.smsPlatform
                this.dataForm.phoneArea = data.mailaccountpool.phoneArea
                this.dataForm.header = data.mailaccountpool.header
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/mailaccountpool/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'account': this.dataForm.account,
                'password': this.dataForm.password,
                'code': this.dataForm.code,
                'phone': this.dataForm.phone,
                'type': this.dataForm.type,
                'defaultWeight': this.dataForm.defaultWeight,
                'health': this.dataForm.health,
                'state': this.dataForm.state,
                'createTime': this.dataForm.createTime,
                'smsPlatform': this.dataForm.smsPlatform,
                'phoneArea': this.dataForm.phoneArea,
                'header': this.dataForm.header
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
